//
//  MyLineController.m
//  HarmonizePub
//
//  Created by rzhy on 16/8/24.
//  Copyright © 2016年 Zy. All rights reserved.
//

#import "MyLineController.h"
#import "Config.h"
#import <UIScrollView+EmptyDataSet.h>
#import "MyLineTableView.h"
#import "CourtTableView.h"

@interface MyLineController () <UIScrollViewDelegate, DZNEmptyDataSetSource, DZNEmptyDataSetDelegate>
{
    MyLineTableView *_listView;
    CourtTableView *_tableView;
}
@property (weak, nonatomic) IBOutlet UISegmentedControl *segmentedControl;
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;

@end

@implementation MyLineController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"排队叫号";
    self.scrollView.contentSize = CGSizeMake(XHScreenWidth * 2, 0.0);
    _listView.emptyDataSetSource = self;
    _listView.emptyDataSetDelegate = self;
    [self getData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    CGFloat viewX = 10.0f;
    CGFloat viewW = XHScreenWidth - viewX * 2;
    _listView.frame = CGRectMake(viewX, 0, viewW, self.scrollView.frame.size.height);
    _tableView.frame = CGRectMake(viewX + XHScreenWidth, 0, viewW, self.scrollView.frame.size.height);
}

- (void)getData {
    [self createTableView];
    NSDictionary *cookie = @{CookieVaule : [UserDefaultsTool stringForKey:XHToken]};
    [HTTPSTool POSTRequestWithPath:XHMyDpQueue cookie:cookie params:nil success:^(NSDictionary * _Nonnull JSON) {
//        XHLog(@"个人排队 -> JSON: %@", JSON);
        _listView.dataArray = JSON[@"data"][@"list"];
    }];
    [HTTPSTool POSTRequestWithPath:XHAllDpQueue cookie:cookie params:nil success:^(NSDictionary * _Nonnull JSON) {
//        XHLog(@"全院排队 -> JSON: %@", JSON);
        _tableView.dataArray = JSON[@"data"][@"list"];
    }];
}

#pragma mark - create tableView
- (void)createTableView {
    _listView = [MyLineTableView tableView];
    [self.scrollView addSubview:_listView];
    
    _tableView = [CourtTableView tableView];
    [self.scrollView addSubview:_tableView];
}

#pragma mark - 切换状态
- (IBAction)swicthState:(UISegmentedControl *)sender {
    // 0:我的排队   1:全院排队
    [self.scrollView setContentOffset:CGPointMake(sender.selectedSegmentIndex * XHScreenWidth, 0.0) animated:YES];
}

#pragma mark - scrollView delegate
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    CGFloat contentX = scrollView.contentOffset.x;
    self.segmentedControl.selectedSegmentIndex = contentX / XHScreenWidth;
}

#pragma mark - DZNEmptyDataSetSource Method
- (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView {
    UIImage *image = [UIImage imageNamed:@"LoadFailure.png"];
    return image;
}

- (NSAttributedString *)descriptionForEmptyDataSet:(UIScrollView *)scrollView {
    NSString *string = XHEmptyDes;
    NSAttributedString *abs = [[NSAttributedString alloc] initWithString:string attributes:nil];
    return abs;
}

- (CGFloat)verticalOffsetForEmptyDataSet:(UIScrollView *)scrollView {
    return -64.0f;
}

#pragma mark - DZNEmptyDataSetDelegate Method
- (BOOL)emptyDataSetShouldDisplay:(UIScrollView *)scrollView {
    return YES;
}

- (BOOL)emptyDataSetShouldAllowTouch:(UIScrollView *)scrollView {
    return YES;
}

- (BOOL)emptyDataSetShouldAllowScroll:(UIScrollView *)scrollView {
    return YES;
}

- (void)emptyDataSet:(UIScrollView *)scrollView didTapView:(UIView *)view {
    [self getData];
}

@end
